/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dao.LocationDAO;
import east2west.dao.TourServiceDAO;
import east2west.dao.TourTypeDAO;
import east2west.dto.LocationDTO;
import east2west.dto.SchedulesDTO;
import east2west.dto.SchedulesLocationDTO;
import east2west.dto.TourDTO;
import east2west.dto.TourDetailDTO;
import east2west.dto.TourOrderDTO;
import east2west.dto.TourPriceDTO;
import east2west.dto.TourServiceDTO;
import east2west.dto.TourTypeDTO;
import east2west.service.TourService;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author anhtuan
 */
public class AdminTourAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String SUCCESS1 = "success1";
    private final static String SER = "ser";
    private final static String SERTOUR = "sertour";
    private final static String LOCALTION = "localtion";
    private final static String TRUE = "true";
    private final static String TRUE3 = "true3";
    private final static String TRUE1 = "true1";
    private final static String TRUE2 = "true2";
    private final static String ERR = "err";
    private final static String NEWSCHEDULE = "newschedule";
    private final static String ADDNEWSCHEDULE = "addnewschedule";
    TourService tourSer = null;
    float sumPrice = 0;
    float min = 0;
    float max = 0;

    public ActionForward listTour(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();
        ArrayList<TourDTO> listTour = new ArrayList<TourDTO>();
        ArrayList<TourDTO> listTourMinMax = new ArrayList<TourDTO>();
        listTour = tourSer.readByAll();
        for (Iterator<TourDTO> it = listTour.iterator(); it.hasNext();) {
            TourDTO tourDTO = it.next();
            int id = tourDTO.getTourId();
            max = tourSer.getMax(id);
            min = tourSer.getMin(id);
            tourDTO.setMin(min);
            tourDTO.setMax(max);
            listTourMinMax.add(tourDTO);

        }

        request.setAttribute("listTour", listTourMinMax);

        request.setAttribute("page", "tourAd.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward listabout(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.setAttribute("page", "about.page");
        return mapping.findForward(SUCCESS1);
    }

    public ActionForward addnewTour(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();
        ArrayList<TourTypeDTO> listTourType = tourSer.readByTourType();
        ArrayList<TourServiceDTO> listService = tourSer.readByTourService();
        ArrayList<LocationDTO> listLocaltion = tourSer.readByAllLocation();
        request.setAttribute("listLocaltion", listLocaltion);
        request.setAttribute("listService", listService);
        request.setAttribute("listTourType", listTourType);
        request.setAttribute("page", "tourAddnew.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward listTourService(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();

        ArrayList<TourServiceDTO> listService = tourSer.readByTourService();
        request.setAttribute("listService", listService);
        return mapping.findForward(SER);
    }

    public ActionForward listLocaltion(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();

        ArrayList<LocationDTO> listLocaltion = tourSer.readByAllLocation();
        request.setAttribute("listLocaltion", listLocaltion);
        request.setAttribute("page", "tourAddnew.page");
        return mapping.findForward(LOCALTION);
    }

    public ActionForward editTour(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();
        int id = Integer.parseInt(request.getParameter("tourId"));
        TourDTO tourDTO = tourSer.findById(id);
        ArrayList<TourTypeDTO> listTourType = tourSer.readByTourType();
        ArrayList<TourPriceDTO> listPrice = tourSer.listavgPrice(id);
        ArrayList<LocationDTO> listLocaltion = tourSer.readByAllLocation();
        ArrayList<TourServiceDTO> listService = tourSer.getServiceName(id);
        ArrayList<SchedulesLocationDTO> listSchedules = tourSer.showLocationTourName(id);
        ArrayList<TourServiceDTO> listAllService = tourSer.readByTourService();
        request.setAttribute("listAllService", listAllService);
        request.setAttribute("listService", listService);
        request.setAttribute("listSchedules", listSchedules);
        request.setAttribute("listLocaltion", listLocaltion);
        request.setAttribute("listPrice", listPrice);
        request.setAttribute("listTourType", listTourType);
        request.setAttribute("tourDTO", tourDTO);
        request.setAttribute("page", "tourEdit.page");
        return mapping.findForward(SUCCESS);
    }

    /**
    TourType
     */
    public ActionForward listTourType(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TourTypeDAO dao = new TourTypeDAO();
        ArrayList<TourTypeDTO> listTourType = dao.readByAll();
        request.setAttribute("listTour", listTourType);
        request.setAttribute("page", "tourType.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward insertTourtype(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TourTypeDTO dto = new TourTypeDTO();
        TourTypeDAO dao = new TourTypeDAO();
        dto.setTourTypeName(request.getParameter("frtypename"));

        if (dao.create(dto)) {
            return mapping.findForward(TRUE3);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteTourType(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("frtypeidd"));
        TourTypeDAO dao = new TourTypeDAO();
        if (dao.delete(id)) {
            return mapping.findForward(TRUE3);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward updateTourType(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TourTypeDTO dto = new TourTypeDTO();
        TourTypeDAO dao = new TourTypeDAO();
        dto.setTourTypeId(Integer.parseInt(request.getParameter("frtypeidd")));
        dto.setTourTypeName(request.getParameter("frtypename"));

        if (dao.update(dto)) {
            return mapping.findForward(TRUE3);
        } else {
            return mapping.findForward(ERR);
        }
    }

    /**
    TourService
     */
    public ActionForward listTourServices(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TourServiceDAO dao = new TourServiceDAO();
        ArrayList<TourServiceDTO> listTourSer = dao.readByAl();
        request.setAttribute("listService", listTourSer);
        request.setAttribute("page", "tourService.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward insertTourService(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TourServiceDTO dto = new TourServiceDTO();
        TourServiceDAO dao = new TourServiceDAO();
        dto.setTourServiceName(request.getParameter("frmodelname"));
        dto.setDescription(request.getParameter("frmodeldes"));
        if (dao.create(dto)) {
            return mapping.findForward(TRUE1);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteTourService(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("frmodelidd"));
        TourServiceDAO dao = new TourServiceDAO();
        if (dao.delete(id)) {
            return mapping.findForward(TRUE1);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteTour(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("tourId"));
        if (tourSer.deleteTour(id)) {
            return mapping.findForward(TRUE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward updateTourService(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TourServiceDTO dto = new TourServiceDTO();
        TourServiceDAO dao = new TourServiceDAO();
        dto.setTourServiceId(Integer.parseInt(request.getParameter("frmodelidd")));
        dto.setTourServiceName(request.getParameter("frmodelname"));
        dto.setDescription(request.getParameter("frmodeldes"));

        if (dao.update(dto)) {
            return mapping.findForward(TRUE1);
        } else {
            return mapping.findForward(ERR);
        }
    }

    /**
    Location
     */
    public ActionForward listTourLocation(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LocationDAO dao = new LocationDAO();
        ArrayList<LocationDTO> listlo = dao.readByAll();
        request.setAttribute("listlocation", listlo);
        request.setAttribute("page", "location.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward insertTourLocation(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LocationDTO dto = new LocationDTO();
        LocationDAO dao = new LocationDAO();
        dto.setLocationName(request.getParameter("frmodelname"));
        dto.setDescription(request.getParameter("frmodeldes"));
        if (dao.create(dto)) {
            return mapping.findForward(TRUE2);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteTourLocation(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("frmodelidd"));
        LocationDAO dao = new LocationDAO();
        if (dao.delete(id)) {
            return mapping.findForward(TRUE2);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward updateTourLocation(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LocationDTO dto = new LocationDTO();
        LocationDAO dao = new LocationDAO();
        dto.setLocationId(Integer.parseInt(request.getParameter("frmodelidd")));
        dto.setLocationName(request.getParameter("frmodelname"));
        dto.setDescription(request.getParameter("frmodeldes"));

        if (dao.update(dto)) {
            return mapping.findForward(TRUE2);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward UpdateTour(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();
        TourDTO tourDTO = new TourDTO();
        int tourId = Integer.parseInt(request.getParameter("tourId"));
        tourDTO.setTourId(tourId);
        tourDTO.setTourName(request.getParameter("tourName"));
        tourDTO.setIntroduction(request.getParameter("intro"));
        tourDTO.setTypeId(Integer.parseInt(request.getParameter("type")));
        tourDTO.setDeparture(request.getParameter("Departure"));
        tourDTO.setDestination(request.getParameter("Destination"));
        tourDTO.setDuration(Integer.parseInt(request.getParameter("Duration")));
        ArrayList<TourPriceDTO> listpriceDTO = new ArrayList<TourPriceDTO>();
        for (int i = 1; i < 5; i++) {
            TourPriceDTO priceDTO = new TourPriceDTO();
            priceDTO.setTourPriceId(Integer.parseInt(request.getParameter("priceId" + i)));
            priceDTO.setPrice(Float.parseFloat(request.getParameter(i + "per")));
            listpriceDTO.add(priceDTO);
        }
        //request.setAttribute("listpriceDTO", listpriceDTO);
        boolean flag = true;
        if (tourSer.update(tourDTO)) {
            for (int i = 1; i < 5; i++) {
                TourPriceDTO priceDTO = new TourPriceDTO();
                priceDTO.setTourPriceId(Integer.parseInt(request.getParameter("priceId" + i)));
                priceDTO.setPrice(Float.parseFloat(request.getParameter(i + "per")));
                if (!tourSer.update(priceDTO)) {
                    flag = false;
                }

            }
            if (flag) {
                return mapping.findForward(TRUE);
            } else {
                return mapping.findForward(ERR);
            }

        } else {
            return mapping.findForward(ERR);
        }

    }

    public ActionForward newSchedule(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("tourId"));
        tourSer = new TourService();
        ArrayList<LocationDTO> listLocaltion = tourSer.readByAllLocation();
        request.setAttribute("tourId", id);
        request.setAttribute("listLocaltion", listLocaltion);
        return mapping.findForward(NEWSCHEDULE);
    }

    public ActionForward AddSchedule(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        SchedulesDTO dto = new SchedulesDTO();
        tourSer = new TourService();
        dto.setTourId(Integer.parseInt(request.getParameter("TourId")));
        dto.setLocationId(Integer.parseInt(request.getParameter("localtion")));
        dto.setDescription(request.getParameter("desSche"));

        if (tourSer.creatSchedule(dto)) {
            request.setAttribute("tourId", Integer.parseInt(request.getParameter("TourId")));
            return mapping.findForward(ADDNEWSCHEDULE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteSchedule(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        SchedulesDTO dto = new SchedulesDTO();
        tourSer = new TourService();
        int scheduleId = Integer.parseInt(request.getParameter("scheduleId"));
        if (tourSer.deleteSchedule(scheduleId)) {
            request.setAttribute("tourId", Integer.parseInt(request.getParameter("tourId")));
            return mapping.findForward(ADDNEWSCHEDULE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward UpdateSchedule(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();
        boolean flag = true;
        int id = Integer.parseInt(request.getParameter("tourId"));
        ArrayList<SchedulesLocationDTO> listSchedules = tourSer.showLocationTourName(id);
        for (Iterator<SchedulesLocationDTO> it = listSchedules.iterator(); it.hasNext();) {
            SchedulesLocationDTO schedulesLocationDTO = it.next();
            SchedulesDTO dto = new SchedulesDTO();
            dto.setSchedulesId(schedulesLocationDTO.getSchedulesId());
            dto.setLocationId(Integer.parseInt(request.getParameter("localtion" + schedulesLocationDTO.getSchedulesId())));
            dto.setDescription(request.getParameter("ScheDes" + schedulesLocationDTO.getSchedulesId()));
            if (!tourSer.update(dto)) {
                flag = false;
            }
        }
        if (flag) {
            request.setAttribute("tourId", id);
            return mapping.findForward(ADDNEWSCHEDULE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward UpdateSevice(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();
        boolean flag = true;
        int id = Integer.parseInt(request.getParameter("tourId"));
        ArrayList<TourServiceDTO> listService = tourSer.getServiceName(id);
        for (Iterator<TourServiceDTO> it = listService.iterator(); it.hasNext();) {
            TourServiceDTO tourService = it.next();
            TourDetailDTO detailDTO = new TourDetailDTO();
            detailDTO.setTourDetailId(tourService.getTourDetailDTO().getTourDetailId());
            detailDTO.setTourServiceId(Integer.parseInt(request.getParameter("ser" + tourService.getTourDetailDTO().getTourDetailId())));
            detailDTO.setDetail(request.getParameter("detail" + tourService.getTourDetailDTO().getTourDetailId()));
            detailDTO.setPrice(Float.parseFloat(request.getParameter("price" + tourService.getTourDetailDTO().getTourDetailId())));
            if (!tourSer.update(detailDTO)) {
                flag = false;
            }
        }
        if (flag) {
            request.setAttribute("tourId", id);
            return mapping.findForward(ADDNEWSCHEDULE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward deleteService(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();
        int detailId = Integer.parseInt(request.getParameter("detailId"));
        if (tourSer.deleteService(detailId)) {
            request.setAttribute("tourId", Integer.parseInt(request.getParameter("tourId")));
            return mapping.findForward(ADDNEWSCHEDULE);
        } else {
            return mapping.findForward(ERR);
        }
    }

    public ActionForward listServiceTour(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        tourSer = new TourService();
        ArrayList<TourServiceDTO> listService = tourSer.readByTourService();
        request.setAttribute("listService", listService);
        request.setAttribute("tourId", Integer.parseInt(request.getParameter("tourId")));
        return mapping.findForward(SERTOUR);
    }

    public ActionForward listTourOrder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TourService serTour = new TourService();
        ArrayList<TourOrderDTO> orderTourDTO = serTour.readOrder();
        request.setAttribute("orderTourDTO", orderTourDTO);
        request.setAttribute("page", "tourAdOrder.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward acceptOrder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        TourService serTour = new TourService();
        if (serTour.acceptOrder(id)) {
            ArrayList<TourOrderDTO> orderTourDTO = serTour.readOrder();
            request.setAttribute("orderTourDTO", orderTourDTO);
        }
        ArrayList<TourOrderDTO> orderTourDTO = serTour.readOrder();
        request.setAttribute("orderTourDTO", orderTourDTO);
        request.setAttribute("page", "tourAdOrder.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward removeOrder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        TourService serTour = new TourService();
        if (serTour.deleteOrder(id)) {
            ArrayList<TourOrderDTO> orderTourDTO = serTour.readOrder();
            request.setAttribute("orderTourDTO", orderTourDTO);
        }
        ArrayList<TourOrderDTO> orderTourDTO = serTour.readOrder();
        request.setAttribute("orderTourDTO", orderTourDTO);
        request.setAttribute("page", "tourAdOrder.page");
        return mapping.findForward(SUCCESS);
    }
}
